//
//  MymovieViewController.m
//  UI-21-豆豆豆瓣
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Wang Hao. All rights reserved.
//

#import "MymovieViewController.h"
#import "DatabaseSingleton.h"
#import "MovieDetailViewController.h"

@interface MymovieViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableView;


@end

@implementation MymovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"电影收藏";
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667-68) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [_myTableView release];
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"mymovie"];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *nameU = [[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"];
    NSMutableArray *array = [[DatabaseSingleton shareDatabaseSingleton] selectcollect:nameU];
    return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndent = @"mymovie";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
    NSString *nameU = [[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"];
    NSMutableArray *array = [[DatabaseSingleton shareDatabaseSingleton] selectData:nameU];
    
    Movie *mov = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = mov.movieName;
    return cell;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    MovieDetailViewController *movDetail = [[MovieDetailViewController alloc] init];
    
    NSMutableArray *array1 = [[DatabaseSingleton shareDatabaseSingleton]selectData:[[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"]];
    Movie *mov1 = [array1 objectAtIndex:indexPath.row];
    movDetail.movie = mov1;
    [self.navigationController pushViewController:movDetail animated:YES];
    
    
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}
//询问哪行需要编辑 都编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}
//每行编辑的样式  全删
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}
//实现逻辑编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //删除数据
    NSMutableArray *array = [[DatabaseSingleton shareDatabaseSingleton] selectcollect:[[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"]];
    Movie *mov = [array objectAtIndex:indexPath.row];
    [[DatabaseSingleton shareDatabaseSingleton] deletecollect:mov.release_date];
    [array removeObjectAtIndex:indexPath.row];
    //删除cell
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationBottom];
    
}
//设置删除按钮文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"上山打老虎";
}
//移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    NSMutableArray *array = [[DatabaseSingleton shareDatabaseSingleton] selectcollect:[[NSUserDefaults standardUserDefaults] objectForKey:@"用户名"]];
    Movie *movi = [array objectAtIndex:sourceIndexPath.row];
    [array removeObjectAtIndex:sourceIndexPath.row];
    [array insertObject:movi atIndex:destinationIndexPath.row];
    [movi release];
    
}
//设置跨区不能移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section){
        
        return proposedDestinationIndexPath;
    }
    return sourceIndexPath;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
